﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver.Builders;
using YmtAuth.Domain.Model.LoginAuth;
using YmtAuth.Domain.Repository;
using YmtSystem.CrossCutting;

namespace YmtUserAuth.DataImport2
{
    class SyncUserBills
    {
        public static void ExecuteSyncUserBills()
        {
            var filePath = System.Configuration.ConfigurationManager.AppSettings["data"];
            var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
            Console.WriteLine("执行导入数据");
            var watch = Stopwatch.StartNew();
            var fields = ReadFile.SyncRead(fullPath);
            var data = FieldToUserBills.Adapter(fields);
            Console.WriteLine("count:{0}", data.Count());
            Console.Read();
           
            YmatouFramework.Start();
            var server = LocalServiceLocator.GetService<IUserLoginBillsRepository>();
            var index = 0;
            foreach (var item in data)
            {
                Console.WriteLine(index++);
                try
                {
                    var userBills = new UserLoginBill(item);
                    var dbUser = server.FindOne(Query<UserLoginBill>.EQ(e => e.UserId, userBills.UserId), "Ymt_Auth_201406", "ULC_201406", true);
                    if (dbUser == null)
                    {
                        //try
                        //{
                        //    server.Add(dbUser, "Ymt_Auth_201406", "ULC_201406");
                        //}
                        //catch (Exception ex)
                        //{
                        //    Console.WriteLine("写入异常{0}", dbUser.UserId);
                        //    Console.WriteLine(ex.ToString());
                        //}
                        var path2 = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "userid_notfind-admin-1.csv");
                        System.IO.File.AppendAllText(path2, string.Format("{0},{1},{2},{3},{4},{5}\r\n"
                            , userBills.UserId
                            , userBills.LoginPassword
                            , userBills.TradingPassword
                            , userBills.LoginBills.LoginId
                            , userBills.LoginBills.Mobile
                            , userBills.LoginBills.Email));
                    }
                    else
                    {
                        if (dbUser.LoginPassword != userBills.LoginPassword
                            || dbUser.LoginBills.Mobile != userBills.LoginBills.Mobile
                            || dbUser.LoginBills.Email != userBills.LoginBills.Email)
                        {
                            var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "backUPUser-admin-1.csv");
                            System.IO.File.AppendAllText(path, string.Format("{0},{1},{2},{3},{4},{5}\r\n"
                                , dbUser.UserId
                                , dbUser.LoginPassword
                                , dbUser.TradingPassword
                                , dbUser.LoginBills.LoginId
                                , dbUser.LoginBills.Mobile
                                , dbUser.LoginBills.Email));

                            var list = new List<UpdateBuilder<UserLoginBill>>();
                            list.Add(Update<UserLoginBill>.Set(e => e.LoginPassword, userBills.LoginPassword));
                            if (!string.IsNullOrEmpty(userBills.LoginBills.Email))
                                list.Add(Update<UserLoginBill>.Set(e => e.LoginBills.Email, userBills.LoginBills.Email));
                            if (!string.IsNullOrEmpty(userBills.LoginBills.Mobile))
                                list.Add(Update<UserLoginBill>.Set(e => e.LoginBills.Mobile, userBills.LoginBills.Mobile));
                            if (!string.IsNullOrEmpty(userBills.TradingPassword))
                                list.Add(Update<UserLoginBill>.Set(e => e.TradingPassword, userBills.TradingPassword));

                            var up = Update.Combine(list);


                            var existsUser = server.FindOne(Query<UserLoginBill>.EQ(e => e.LoginBills.LoginId, userBills.LoginBills.LoginId), "Ymt_Auth_201406", "ULC_201406", true);
                            if (existsUser != null)
                            {
                                server.Update(Query<UserLoginBill>.EQ(e => e.LoginBills.LoginId, userBills.LoginBills.LoginId), up);
                            }
                            else
                            {
                                var path3 = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "loginId_notfind-admin-1.csv");
                                System.IO.File.AppendAllText(path3, string.Format("{0},{1},{2},{3},{4},{5}\r\n"
                                    , userBills.UserId
                                    , userBills.LoginPassword
                                    , userBills.TradingPassword
                                    , userBills.LoginBills.LoginId
                                    , userBills.LoginBills.Mobile
                                    , userBills.LoginBills.Email));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.Read();
                }
            }

            Console.WriteLine("end");
            Console.Read();
        }
    }
}
